const { ccclass } = cc._decorator;

@ccclass
export default class WxUtils {

    public static createAuthorizeBtn(btnNode: cc.Node, onTap: (res: any) => void): wx.UserInfoButton {
        let btnSize = cc.size(btnNode.width + 10, btnNode.height + 10);
        let frameSize = cc.view.getFrameSize();
        let winSize = cc.winSize;
        console.log("winSize: ", winSize);
        console.log("frameSize: ", frameSize);
        //适配不同机型来创建微信授权按钮
        let left = (winSize.width * 0.5 + btnNode.x - btnSize.width * 0.5) / winSize.width * frameSize.width;
        let top = (winSize.height * 0.5 - btnNode.y - btnSize.height * 0.5) / winSize.height * frameSize.height;
        let width = btnSize.width / winSize.width * frameSize.width;
        let height = btnSize.height / winSize.height * frameSize.height;
        console.log("button pos: ", cc.v2(left, top));
        console.log("button size: ", cc.size(width, height));

        let wxBtn: wx.UserInfoButton = wx.createUserInfoButton({
            type: 'text',
            text: '',
            style: {
                left: left,
                top: top,
                width: width,
                height: height,
                lineHeight: 0,
                backgroundColor: '',
                color: '#ffffff',
                textAlign: 'center',
                fontSize: 16,
                borderRadius: 4
            }
        });

        wxBtn.onTap((res) => {
            if (onTap) {
                onTap(res);
            }
        });
        return wxBtn;
    }
}
